package com.base.generic;

import com.base.pojo.Apple;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * 解析类的参数泛型
 *
 * @author: May
 * @date 2020/4/15
 */
abstract public class MyClassGeneric<T> {

    private Class<T> beanClass;

    public MyClassGeneric() {
        // 此方式，需要在子父类环境下使用
        ParameterizedType parameterizedType = (ParameterizedType) this.getClass().getGenericSuperclass();
        Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
        this.beanClass = (Class<T>) actualTypeArguments[0];
    }

    public String getClassGeneric() throws IllegalAccessException, InstantiationException {
        return beanClass.getCanonicalName();
    }

    public static void main(String[] args) throws InstantiationException, IllegalAccessException {
        new MyClassGeneric<Apple>() {

        }.getClassGeneric();
    }
}
